﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Diagnostics;

/*
 * represents the screen of the vending machine
 */

namespace vmMachine
{

    class MoneyDisplay
    {
        private static System.Timers.Timer aTimer;
        private static VendingMachineLogic aMachine;
        private Form1 form;
        public int speed = 3000;
        Stopwatch stopWatch;


        public MoneyDisplay(VendingMachineLogic machine, Form1 f)
        {
            aMachine = machine;
            this.form = f;
            
            //update the display&change to current money amount
            form.Display.Text = aMachine.MoneyToString();
            form.ChangeCounter.Text = aMachine.changeToString();

            //create a new timer
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            
            // Set the Interval to 5 seconds.
            aTimer.Interval = speed;
            
            stopWatch = new Stopwatch();
        }

        //this method is incharge of what happends when the timers reaches it's end
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            stopWatch.Stop();
            stopWatch.Reset();
            form.Display.Text = aMachine.MoneyToString();
            aMachine.resetChange();
            form.ChangeCounter.Text = aMachine.changeToString();
        }

        public void updateDisplay()
        {
            aTimer.Stop();  //stop the timer
            aTimer.Interval = speed;
            form.Display.Text = aMachine.MachineDisplay; //update the form with the machineDisplay
            form.ChangeCounter.Text = aMachine.changeToString(); //update the form with change display
            aTimer.Start();
            stopWatch.Start();
        }

        public void pause()
        {
            stopWatch.Stop();
            aTimer.Stop();
        }

        public void resume()
        {
            long elapsed = stopWatch.Elapsed.Milliseconds;
            stopWatch.Reset();
            aTimer.Interval = aTimer.Interval - (int)elapsed;
            aTimer.Start();

        }


    }
}